package cn.deepmax.anime.web;


import cn.deepmax.anime.web.pojo.Section;
import cn.deepmax.anime.web.redis.RedisTemplate;
import cn.deepmax.anime.web.support.PhantomJSDriverFactory;
import cn.deepmax.anime.web.support.WebDriverPool;
import cn.deepmax.anime.web.task.SectionPageTask;
import cn.deepmax.anime.web.task.SectionTask;
import cn.deepmax.anime.web.task.SectionTotalTask;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

@Slf4j
public class MainEntry {


    private Integer threadSize;

    public MainEntry(Integer threadSize) {
        this.threadSize = threadSize;
    }

    public static void main(String[] args) throws Exception{
        MainEntry entry = new MainEntry(16);
        entry.runtask();
    }

    public void runtask() throws Exception{
        initRootFolder();
        WebDriverPool<PhantomJSDriver> pool = new WebDriverPool<>(threadSize, new PhantomJSDriverFactory());

        ExecutorService executorService = Executors.newFixedThreadPool(threadSize);
        SectionTask task = new SectionTask();
        List<Section> sectionList = task.getSection();
        RedisTemplate redisTemplate = new RedisTemplate();
        CountDownLatch latch = new CountDownLatch(sectionList.size());
        sectionList.forEach(section -> {
            executorService.submit(new SectionTotalTask(section, pool, redisTemplate,latch));
        });
        latch.await();
        log.info("Section page total get OK! total section size is [{}]", sectionList.size());
        int pageTaskLen = sectionList.stream().mapToInt(Section::getTotal).sum();
        CountDownLatch pageLatch = new CountDownLatch(pageTaskLen);
        List<SectionPageTask> allPageTask = new ArrayList<>();
        sectionList.forEach(section -> {
            for (int i = 1; i <= section.getTotal(); i++) {
                allPageTask.add(new SectionPageTask(section, pool, redisTemplate, pageLatch, i) );
            }
        });


        log.info("Total page task size is {}.", pageTaskLen);

        allPageTask.forEach(pageTask->{
            executorService.submit(pageTask);
        });
        pageLatch.await();
        log.info("*********All page Task get OK! size is {}", pageTaskLen);
        executorService.shutdown();
        redisTemplate.close();
        pool.close();
        log.info("*********Close all resources" );

    }


    private void initRootFolder(){
        File root = new File(Constant.getRootFolder());
        if(!root.exists())
            root.mkdirs();
    }


}
